import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/blind_can_transfer_page_model.dart';
import 'package:yysk_app/model/user_prop_model/list_can_transfer_page_model.dart';
import 'package:yysk_app/pages/nft_pages/danation/danation_confirm_widget.dart';
import 'package:yysk_app/pages/nft_pages/my_prop/prop_batch_gift/prop_batch_gift_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/app_bar_back_widget.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:oktoast/oktoast.dart';

class PropBatchGiftPage extends GetView<PropBatchGiftController> {
  static const path = '/propBatchGift';
  const PropBatchGiftPage({super.key});

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;

    return Obx(
      () => Scaffold(
        body: Column(
          children: [
            Expanded(
              child: Stack(
                children: [
                  SizedBox(
                    width: context.width,
                    child: ImageAssetWidget(
                      width: 375.px,
                      height: 196.px,
                      alignment: Alignment.topCenter,
                      imageUrl: 'static/images/prop_transfer_bg.png',
                    ),
                  ),

                  Container(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                    child: Column(
                      children: [
                        SizedBox(height: top),
                        // CustomAppBar2(title: '批量转赠', context: context),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            AppBarBackWidget(
                              context: context,
                              onBack: () {},
                              backgroundColor: AppColors.backgroundColorBlack,
                            ),
                            Text(
                              '批量转赠',
                              style: TextStyle(
                                fontSize: 18.px,
                                color: AppColors.fontColorBlack,
                              ),
                            ),
                            Container(
                              width: 27.px,
                              height: 27.px,
                              margin: EdgeInsets.only(right: 21.px),
                              padding: EdgeInsets.fromLTRB(
                                4.px,
                                5.px,
                                4.px,
                                5.px,
                              ),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8.px),
                                color: AppColors.backgroundColor,
                              ),
                              child: GestureDetector(
                                onTap: () {
                                  controller.selectAll();
                                },
                                child: ImageAssetWidget(
                                  width: 27.px,
                                  height: 27.px,
                                  imageUrl:
                                      'static/images/prop/all_select_ico.png',
                                ),
                              ),
                            ),
                          ],
                        ),

                        Expanded(
                          child: Container(
                            padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
                            child: RefreshAndScrollWidget(
                              onRefresh: _handleRefresh,
                              scrollController: controller.scrollController,
                              itemBuilder:
                                  (item, index) =>
                                      controller.transferType.value == '3'
                                          ? _listItem(
                                            item as ListCanTransferPageItem,
                                            index,
                                          )
                                          : _listItem2(
                                            item as BlindCanTransferPageItem,
                                            index,
                                          ),
                              itemCount:
                                  controller.transferType.value == '3'
                                      ? controller.porpList.length
                                      : controller.blindList.length,
                              items:
                                  controller.transferType.value == '3'
                                      ? controller.porpList
                                      : controller.blindList,
                            ),
                          ),
                        ),
                        Text(
                          '${controller.drawerCount.value}',
                          style: TextStyle(fontSize: 0),
                        ), //重新构图用
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: 375.px,
              height: 120.px,
              padding: EdgeInsets.only(top: 20.px),
              child: Stack(
                children: [
                  Positioned(
                    right: 0,
                    bottom: 0,
                    child: ImageAssetWidget(
                      imageUrl: 'static/images/bottom_bg.png',
                      width: 258,
                      height: 74,
                    ),
                  ),
                  Positioned(
                    left: 21.px,
                    top: 20.px,
                    child: RichText(
                      text: TextSpan(
                        children: [
                          TextSpan(
                            text: '已选中：',
                            style: TextStyle(
                              color: Color(0xFF222222),
                              fontSize: 14.px,
                            ),
                          ),
                          TextSpan(
                            text: '${controller.num.value}',
                            style: TextStyle(
                              color: AppColors.fontColorBlack,
                              fontSize: 24.px,
                              fontWeight: FontWeight.w700,
                            ),
                          ),
                          TextSpan(
                            text: '条',
                            style: TextStyle(
                              color: Color(0xFF222222),
                              fontSize: 14.px,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Positioned(
                    right: 21.px,
                    top: 0.px,
                    child: ButtonWidget(
                      label: '批量转赠',
                      width: 172.px,
                      height: 54.px,
                      imageBg: 'static/images/batch_btn_bg.png',
                      contentType: ContentType.other,
                      paddingText: EdgeInsets.only(left: 16.px),
                      onPressed: () {
                        if (controller.transferType.value == '3') {
                          List<dynamic> list = [];
                          String url = '';
                          int propId = 0;
                          for (var element in controller.porpList) {
                            if (element.isSelect == true) {
                              list.add(element.id);
                              url = element.propImageUrl ?? '';
                              propId = element.propId ?? 0;
                            }
                          }
                          if (list.isEmpty) {
                            showToast('请选择转赠道具');
                            return;
                          }
                          Get.toNamed(
                            DanationConfirmWidget.path,
                            arguments: {
                              "list": list,
                              'getTokentype': '3',
                              'url': url,
                              'propId': propId,
                              "type": controller.type.value,
                              "transferTypeWay": 3,
                            },
                          );
                        } else {
                          List<dynamic> list = [];
                          String url = '';
                          int boxId = 0;
                          for (var element in controller.blindList) {
                            if (element.isSelect == true) {
                              list.add(element.id);
                              url = element.boxImageUrl ?? '';
                              boxId = element.boxId ?? 0;
                            }
                          }
                          if (list.isEmpty) {
                            showToast('请选择转赠心愿盒');
                            return;
                          }
                          Get.toNamed(
                            DanationConfirmWidget.path,
                            arguments: {
                              "list": list,
                              'getTokentype': '2',
                              'url': url,
                              'boxId': boxId,
                              "type": controller.type.value,
                              "transferTypeWay": 2,
                            },
                          );
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _listItem(ListCanTransferPageItem item, dynamic index) {
    return Container(
      height: 90.px,
      padding: EdgeInsets.fromLTRB(12.px, 12.px, 21.px, 12.px),
      margin: EdgeInsets.only(bottom: 12.px),
      decoration: BoxDecoration(
        color: AppColors.backgroundColorLightGrey,
        borderRadius: BorderRadius.circular(8.px),
      ),
      child: Row(
        children: [
          LazyImage(
            imageUrl: '${item.propImageUrl}',
            height: 65.px,
            width: 65.px,
            borderRadius: 4.px,
          ),
          hiSpace(width: 16.px),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                width: 200.px,
                child: Text(
                  '${item.title}',
                  style: TextStyle(
                    fontSize: 18.px,
                    color: AppColors.backgroundColorBlack,
                    fontFamily: 'CustomFont',
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
              Text(
                '有效时间：${item.invalidDate == "2099-12-31 23:59:59" ? '永久有效' : item.invalidDate}',
                style: TextStyle(
                  fontSize: 12.px,
                  color: AppColors.fontColorGrey,
                ),
              ),
            ],
          ),
          Expanded(
            child: GestureDetector(
              onTap: () {
                if (controller.num.value == 20 && item.isSelect == false) {
                  showToast('最大选择20条');
                  return;
                }
                controller.selectedList.value = [];
                var maxCount = 0;
                var chooseNum = 0;
                for (var element in controller.porpList) {
                  if (element.isSelect == true) {
                    if (maxCount < 20) {
                      maxCount += 1;

                      controller.selectedList.add(element.id!);
                    } else if (maxCount == 20 &&
                        controller.porpList[index].isSelect) {
                      controller.porpList[index].isSelect = !item.isSelect;
                      controller.drawerCount.value++;
                      return;
                    } else {
                      showToast('超出最大值20');
                      return;
                    }
                  }
                }

                controller.porpList[index].isSelect = !item.isSelect;
                for (var element in controller.porpList) {
                  if (element.isSelect == true) {
                    chooseNum++;
                  }
                }
                controller.num.value = chooseNum;
                controller.drawerCount.value++;
              },
              child: Container(
                alignment: Alignment.centerRight,
                child:
                    item.isSelect
                        ? ImageAssetWidget(
                          imageUrl: 'static/images/prop/select_ico.png',
                          width: 26.px,
                          height: 27.px,
                        )
                        : ImageAssetWidget(
                          imageUrl: 'static/images/prop/default_ico.png',
                          width: 26.px,
                          height: 27.px,
                        ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _listItem2(BlindCanTransferPageItem item, dynamic index) {
    return Container(
      height: 90.px,
      padding: EdgeInsets.fromLTRB(12.px, 12.px, 21.px, 12.px),
      margin: EdgeInsets.only(bottom: 12.px),
      decoration: BoxDecoration(
        color: AppColors.backgroundColorLightGrey,
        borderRadius: BorderRadius.circular(8.px),
      ),
      child: Row(
        children: [
          LazyImage(
            imageUrl: '${item.boxImageUrl}',
            height: 65.px,
            width: 65.px,
            borderRadius: 4.px,
          ),
          hiSpace(width: 16.px),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '${item.boxName}',
                style: TextStyle(
                  fontSize: 18.px,
                  color: AppColors.backgroundColorBlack,
                  fontFamily: 'CustomFont',
                ),
              ),
              // Text(
              //   '',
              //   style: TextStyle(
              //     fontSize: 12.px,
              //     color: AppColors.fontColorGrey,
              //   ),
              // ),
            ],
          ),
          Expanded(
            child: GestureDetector(
              onTap: () {
                controller.selectedList.value = [];
                var maxCount = 0;
                var chooseNum = 0;
                for (var element in controller.blindList) {
                  if (element.isSelect == true) {
                    if (maxCount < 19) {
                      maxCount += 1;

                      controller.selectedList.add(element.id!);
                    } else if (maxCount == 19 &&
                        controller.blindList[index].isSelect) {
                      controller.blindList[index].isSelect = !item.isSelect;
                      controller.drawerCount.value++;
                      return;
                    } else {
                      showToast('超出最大值20');
                      return;
                    }
                  }
                }

                controller.blindList[index].isSelect = !item.isSelect;
                for (var element in controller.blindList) {
                  if (element.isSelect == true) {
                    chooseNum++;
                  }
                }
                controller.num.value = chooseNum;
                controller.drawerCount.value++;
              },
              child: Container(
                alignment: Alignment.centerRight,
                child:
                    item.isSelect
                        ? ImageAssetWidget(
                          imageUrl: 'static/images/prop/select_ico.png',
                          width: 26.px,
                          height: 27.px,
                        )
                        : ImageAssetWidget(
                          imageUrl: 'static/images/prop/default_ico.png',
                          width: 26.px,
                          height: 27.px,
                        ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _handleRefresh() async {
    if (controller.transferType.value == '3') {
      controller.getPropList();
    } else {
      controller.getBlindList();
    }
    controller.num.value = 0;
    return;
  }
}
